//
//  HTWayBillCell.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/18.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTWayBillCell.h"
#import "HTCommentView.h"

@interface HTWayBillCell()<HTCommentViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIView *tapView;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *stateImageView;
@property (weak, nonatomic) IBOutlet UILabel *iconLabel;

@end

@implementation HTWayBillCell
- (void)setWaybillModel:(HTWayBillModel *)waybillModel{
    _waybillModel = waybillModel;
    self.startLabel.text = waybillModel.from;
    self.endLabel.text = waybillModel.to;
    self.timeLabel.text = [HTUtils timeIntervalExchange:waybillModel.createtime];
    self.nameLabel.text = [NSString stringWithFormat:@"%@(%@)",waybillModel.carname,waybillModel.carnumber];
   //  0 未结单 1 进行中，2 待评价 3.已完成 4 取消
    switch ([waybillModel.state integerValue]) {
        case 0:
        {
            self.tapView.userInteractionEnabled = YES;
            self.iconLabel.text = @"待接单";
            self.stateLabel.text = @"确认接单";
            self.stateImageView.image = [UIImage imageNamed:@"icon_list_comment"];
        }
            break;
        case 1:
        {
            self.tapView.userInteractionEnabled = YES;
            self.iconLabel.text = @"运输中";
            self.stateLabel.text = @"确认到货";
            self.stateImageView.image = [UIImage imageNamed:@"icon_receipt_list"];
            
        }
            break;
        case 2:
        {
            self.tapView.userInteractionEnabled = YES;
            self.iconLabel.text = @"待评价";
            self.stateLabel.text = @"评价司机";
            self.stateImageView.image = [UIImage imageNamed:@"icon_list_comment"];
            
        }
            break;
        case 3:
        {
            self.tapView.userInteractionEnabled = NO;
            self.iconLabel.text = @"已完成";
            self.stateLabel.text = @"查看详情";
            self.stateImageView.image = [UIImage imageNamed:@"icon_list_detail"];
        }
            break;
        case 4:
        {
            self.tapView.userInteractionEnabled = NO;
            self.iconLabel.text = @"已取消";
            self.stateLabel.text = @"查看详情";
            self.stateImageView.image = [UIImage imageNamed:@"icon_list_detail"];
        }
            break;
            
        default:
            break;
    }
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didCall:)];
    [self.tapView addGestureRecognizer:tap];
}
- (void)didCall:(id)sender{
    //  0 未结单 1 进行中，2 待评价 3.已完成 4 取消
    switch ([self.waybillModel.state integerValue]) {
        case 0://确认接单
        {
            [UIAlertController alertStyleShowInController:self.controller title:@"温馨提示" message:@"确定要接单吗？" alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
                [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                     [self netLoadConformReceiveOrder];
                }]];
                
            } completion:^{
                
            }];
            
        }
            break;
        case 1://确认到货
        {
            [UIAlertController alertStyleShowInController:self.controller title:@"温馨提示" message:@"确定到货吗？" alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
                [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self netLoadConformReceive];
                }]];
                
            } completion:^{
                
            }];
            
        }
            break;
        case 2://弹出评价弹框
        {
            HTCommentView *commentView = [[HTCommentView alloc]init];
            commentView.delegate = self;
            [commentView showView];
        }
            break;
        default:
            break;
    }
}
- (void)didCommitComment:(NSString *)message scroll:(NSString *)scroll commentView:(HTCommentView *)commentView{
    YDHUD *hud = [YDHUD juhuaHUD];
    [[UIApplication sharedApplication].keyWindow addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestWaybillCommentWithOrderid:self.waybillModel.ID scroller:scroll impersson:message success:^(id  _Nullable responseObject) {
         [hud hideAnimated:YES];
        DBLog(@"评价：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"评价失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        [commentView dismissView];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
        
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"评价失败"];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

#pragma mark --- 网络请求
- (void)netLoadConformReceive{
    YDHUD *hud = [YDHUD juhuaHUD];
    [[UIApplication sharedApplication].keyWindow addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestWaybillComformReceivedWithId:self.waybillModel.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"确认收货：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"确认收货失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
             [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"确认收货失败"];
    }];
}
- (void)netLoadConformReceiveOrder{
    YDHUD *hud = [YDHUD juhuaHUD];
    [[UIApplication sharedApplication].keyWindow addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestWaybillComformWithOrderid:self.waybillModel.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"确认接单：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"确认接单失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"确认接单失败"];
    }];
}

@end
